local jass = require 'jass.common'
local map = require 'maps.map'
local gap = 1 -- 刷兵间隔
local lastTimer, lastTimeDialog

local function Clean()
	if lastTimer then
		jass.DestroyTimer(lastTimer)
		lastTimer = nil
	end

	if lastTimeDialog then
		jass.DestroyTimerDialog(lastTimeDialog)
		lastTimeDialog = nil
	end
end

local function _createArmyDialog(time)
	local t = jass.CreateTimer()
	local timedialog = jass.CreateTimerDialog(t)
	jass.TimerDialogSetTitle(timedialog, "下一波攻击即将到来...")
	jass.TimerDialogDisplay(timedialog, true)
	jass.TimerStart(t, time, false, function()
	end)
	return t, timedialog
end

--开始刷兵
function map.createArmy()
	if map.army_timer then
		map.army_timer:remove()
		map.army_timer = nil
		return
	end
	local wave = -1
	local rects	= map.rects['刷兵区域']
	map.army_timer = ac.loop(1000 * gap, function(t)
		Clean()
		lastTimer, lastTimeDialog = _createArmyDialog(gap)

		--成长
		wave = wave + 1

		--每30秒刷一波兵
		local name = map.units['小兵类型'][wave]
		if not name then return end
		for i = 1, 3 do
			local t = ac.timer(1000, 1, function()
				local data = ac.lni.unit[name]
				local u = g_playerMgr.com[2]:create_unit(name, rects[i], rects[1]:get_point() / rects[2]:get_point())
				--保存钱和经验奖励
				u.reward_gold = data['金钱']
				u.reward_exp = data['经验']

				--发布移动攻击指令
				u:issue_order('attack', map.rects['基地'])
			end)
			t:on_timer()

			--local x, y = rects[i]:get_point():get()
			--local u = jass.CreateUnit(player.com[2].handle, base.string2id('hfoo'), x, y, 0) print('111  ', base.string2id('hfoo'), player.com[2].handle, x, y, u)
			--x, y = map.rects['基地']:get_point():get()
			--jass.IssuePointOrder(u, 'attack', x, y)

		end
	end)
	map.army_timer:on_timer()
end

ac.game:event(MapEvent.PAUSE_MONSTER_TIMER, function()
	if lastTimer then
		jass.PauseTimer(lastTimer)
		jass.PauseTimer(map.army_timer)
	end
end)